<template>
  <div class="viewContain">
    <el-form :model="listQuery" size="small" label-width="120px">
      <el-row>
        <el-col :xs="{ span: 24 }" :sm="{ span: 6 }" :md="{ span: 6 }" :xl="{ span: 6 }">
          <el-form-item label="商品名称:" prop="goodsName">
            <el-input
              v-model="listQuery.goodsName"
              placeholder="请输入"
              clearable
              @keyup.enter.native="handleSearch"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="{ span: 24 }" :sm="{ span: 6 }" :md="{ span: 6 }" :xl="{ span: 6 }">
          <el-form-item label="商品型号:" prop="goodsModel">
            <el-input
              v-model="listQuery.goodsModel"
              placeholder="请输入"
              clearable
              @keyup.enter.native="handleSearch"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="{ span: 24 }" :sm="{ span: 12 }" :md="{ span: 12 }" :xl="{ span: 12 }">
          <el-form-item label-width="20px">
            <el-button size="mini" type="primary" @click="handleAdd">新增</el-button>
            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
            <el-upload
              action="#"
              :limit="1"
              :on-exceed="handleExceed"
              :before-upload="beforeFileUpload"
              style="display:inline-block"
            >
              <el-button slot="trigger" size="mini" type="primary">导入商品</el-button>
            </el-upload>
            <el-button size="mini" type="primary" plain @click="handleReload">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <Dialog
      :model-value="dailog.visible"
      :title="dailog.title"
      :visible="dailog.visible"
      :width="dailog.width"
      @handleCloseDialog="handleCloseDialog"
      @handleConfirm="handleConfirm"
    >
      <template #content>
        <el-form
          ref="formData"
          label-width="100px"
          :model="formData"
          :rules="rules"
          size="small"
        >
          <el-row>
            <el-form-item label="商品名称 :" prop="goodsName">
              <el-input
                v-model="formData.goodsName"
                clearable
                placeholder="请输入名称"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="商品型号 :" prop="goodsModel">
              <el-input
                v-model="formData.goodsModel"
                clearable
                placeholder="请输入商品型号"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="长度 :" prop="goodsLength">
              <el-input
                v-model="formData.goodsLength"
                clearable
                placeholder="请输入长度"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="宽度 :" prop="goodsWidth">
              <el-input
                v-model="formData.goodsWidth"
                clearable
                placeholder="请输入宽度"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="高度 :" prop="goodsHeight">
              <el-input
                v-model="formData.goodsHeight"
                clearable
                placeholder="请输入高度"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="重量 :" prop="goodsWeight">
              <el-input
                v-model="formData.goodsWeight"
                clearable
                placeholder="请输入重量"
                style="max-width: 200px"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
    </Dialog>
    <el-table
      ref="table"
      v-loading="loading"
      fit
      stripe
      :data="tableData"
      border
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      size="mini"
    >
      <el-table-column align="center" label="序号" type="index">
        <template slot-scope="scope">
          <span>{{ (listQuery.pageNum - 1) * listQuery.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsName" align="left" label="商品名称" width="400" />
      <el-table-column prop="goodsModel" align="center" label="商品型号" />
      <el-table-column prop="goodsLength" align="center" label="长度(cm)" />
      <el-table-column prop="goodsWidth" align="center" label="宽度(cm)" />
      <el-table-column prop="goodsHeight" align="center" label="高度(cm)" />
      <el-table-column prop="goodsVolume" align="center" label="体积(m³)">
        <template slot-scope="scope">
          <!-- {{ ((scope.row.goodsLength * scope.row.goodsWidth * scope.row.goodsHeight)/(100*100*100)) }} -->
          {{ ((scope.row.goodsLength * scope.row.goodsWidth * scope.row.goodsHeight)/(100*100*100)).toFixed(3) }}
        </template>
      </el-table-column>
      <el-table-column prop="goodsWeight" align="center" label="重量(kg)" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button class="text-button" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button class="text-button el-button--danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total-count="totalCount"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
// import { deleteDeliveryType, deliveryList, addDeliveryType, updateDeliveryType } from '@/api/index'
import { goodsList, deleteByIds, goodsAdd, goodsUpdate, importGoods } from '@/api/index'
import pagination from '@/components/pagination/index.vue'
import Dialog from '@/components/dailog/index.vue'
// import { commonMixin } from '@v/mixin/commonMixin.js'

export default {
  components: {
    Dialog, pagination
  },
  // mixins: [commonMixin],
  data() {
    return {
      loading: false,
      options: [],
      addressList: [],
      deliveryList: [],
      totalCount: 0,
      listQuery: {
        pageNum: 1,
        // pageSize: 10,
        pageSize: 10,
        goodsName: ''
      },
      //   tableData: [],
      tableData: [
        {
          id: 1,
          goodsName: 'BF522工业吸尘器',
          goodsModel: 'BF522',
          goodsLength: 10,
          goodsWidth: 1,
          goodsHeight: 2,
          goodsVolume: 3,
          goodsWeight: 4,
          children: [
            {
              id: 11,
              goodsName: '零件主机',
              goodsModel: 'BF522-1',
              goodsLength: 10,
              goodsWidth: 1,
              goodsHeight: 2,
              goodsVolume: 3,
              goodsWeight: 4
            },
            {
              id: 12,
              goodsName: '零件水箱',
              goodsModel: 'BF522-2',
              goodsLength: 10,
              goodsWidth: 1,
              goodsHeight: 2,
              goodsVolume: 3,
              goodsWeight: 4
            },
            {
              id: 13,
              goodsName: '零件手柄',
              goodsModel: 'BF522-3',
              goodsLength: 10,
              goodsWidth: 1,
              goodsHeight: 2,
              goodsVolume: 3,
              goodsWeight: 4
            }
          ]
        },
        {
          id: 2,
          goodsName: 'BF510大型工业吸尘器',
          goodsModel: 'BF522',
          goodsLength: 10,
          goodsWidth: 1,
          goodsHeight: 2,
          goodsVolume: 3,
          goodsWeight: 4
        }
      ],
      rules: {
        goodsName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        goodsModel: [
          { required: true, message: '请输入商品型号', trigger: 'change' }
        ],
        goodsLength: [
          { required: true, message: '请输入长度', trigger: 'change' }
        ],
        goodsWidth: [
          { required: true, message: '请输入宽度', trigger: 'change' }
        ],
        goodsHeight: [
          { required: true, message: '请输入高度', trigger: 'change' }
        ],
        goodsWeight: [
          { required: true, message: '请输入重量', trigger: 'change' }
        ]
      },
      rules1: {},
      formData: {
        id: '',
        goodsName: '',
        goodsModel: '',
        goodsLength: '',
        goodsWidth: '',
        goodsHeight: '',
        goodsWeight: ''
      },
      dailog: {
        // height: '',
        title: '合作快递新增',
        visible: false,
        visibleAssign: false,
        width: '400px'
      },
      uploadFile: null
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 快递列表
    getList() {
    //   this.loading = true
    //   goodsList(this.listQuery).then(res => {
    //     if (res.code === 200) {
    //       this.tableData = res.data.list
    //       this.totalCount = res.data.total
    //       this.loading = false
    //     } else {
    //       this.loading = false
    //     }
    //   })
    },

    // 新增
    handleAdd() {
      // alert('开发中,敬请期待~')
      this.dailog.title = '商品新增'
      this.dailog.visible = true
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 删除
    handleDelete(id) {
      // alert('开发中,敬请期待~')
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // const params = { ids: id }
        const ids = id
        deleteByIds({}, ids).then(res => {
          console.log('res====删除结果', res)
          if (res.code === 200) {
            this.getList()
          }
        })
      })
    },
    handleReload() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10,
        goodsName: ''
      }
      this.getList()
    },
    handleEdit(row) {
      console.log('row===', row)
      this.dailog.visible = true
      this.dailog.title = '编辑'
      this.$nextTick(() => {
        this.formData = JSON.parse(JSON.stringify(row))
      })
    },
    handleConfirm() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          console.log('this.dailog.title===', this.dailog.title)
          this.dailog.visible = false
          if (this.dailog.title === '商品新增') {
            goodsAdd(this.formData).then(res => {
              if (res.code === 200) {
                this.getList()
                // this.$refs['formData'].resetFields()
              }
            })
          } else {
            goodsUpdate(this.formData).then(res => {
              if (res.code === 200) {
                this.getList()
                // this.$refs['formData'].resetFields()
              }
            })
          }
        }
      })
    },
    handleCloseDialog() {
      this.dailog.visible = false
      // this.$refs['formData'].resetFields()
      this.formData = {
        id: '',
        goodsName: '',
        goodsModel: '',
        goodsLength: '',
        goodsWidth: '',
        goodsHeight: '',
        goodsWeight: ''
      }
      console.log(123)
    },
    // 上传多个文件时进行处理
    handleExceed(files, fileList) {
      this.$message.warning(`限制只能上传1个文件`)
    },
    // 上传之前要干的事情
    beforeFileUpload(file) {
      console.log(file)
      if (file) {
        this.isFile = true
        this.$confirm('文件选取成功,是否继续上传?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(res => { this.submitUpload() })
      } else { return }
      const isLt50M = file.size / 1024 / 1024 < 50
      const temp = file.name.split('.')
      if (temp[temp.length - 1] === 'xls' || temp[temp.length - 1] === 'xlsx' || temp[temp.length - 1] === 'zip') {
        if (isLt50M) {
          console.log(this.uploadFile)
          this.uploadFile = file
          console.log(this.uploadFile)
        } else {
          this.$message({
            type: 'error',
            message: '上传文件大小超出50MB!'
          })
        }
      } else {
        this.$message({
          type: 'error',
          message: '上传文件只能是xls或者xlsx文件!'
        })
      }
      return false
    },
    submitUpload() {
      console.log('开始上传excel文件啦')
      if (this.isFile) {
        const params = new FormData()
        params.append('file', this.uploadFile)
        // const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        // params.append('userId', userInfo.userId)
        // const fileType = this.fileType
        // console.log(fileType)
        console.log(params)
        importGoods(params).then(res => {
          console.log('res===', res)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '上传成功'
            })
          } else if (res.code === 500) {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        })
      } else {
        this.$message({
          message: '请选取文件后再上传!',
          type: 'warning'
        })
      }
    }
  }
}
</script>

<style scoped>

</style>

